package com.dtsz.m2.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.CountDownTimer;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Date;

import static android.widget.Toast.LENGTH_LONG;

public class TaskCountdownUtils extends CountDownTimer {
    private TextView mButton;
    private Activity mActivity;
    private String buttonClick;


    public TaskCountdownUtils(Activity activity, TextView button, long millisInFuture,
                              long countDownInterval) {
        super(millisInFuture, countDownInterval);
        mActivity = activity;
        mButton = button;
    }

    @Override
    public void onFinish() {
        // 获取 验证码按钮可点击
        // setButtonEnable();
        mButton.setEnabled(true);
        mButton.setText(buttonClick);

    }

    public String getButtonClick() {
        return buttonClick;
    }

    public void setButtonClick(String buttonClick) {
        this.buttonClick = buttonClick;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        // 获取验证码按钮不可点击,倒计时开始
        setButtonUnEnable(millisUntilFinished);

    }

    @SuppressWarnings("deprecation")
    private void setButtonEnable() {
        mButton.setEnabled(true);
        AlertDialog.Builder dialog = new AlertDialog.Builder((Context) mActivity);
        dialog.setTitle("开采结束");
        //dialog.setMessage(updateDescription);
        dialog.setPositiveButton("返回", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                arg0.dismiss();
                mActivity.finish();
                //showDownloadDialog();
            }
        });
        dialog.create();
        dialog.setCancelable(false);
        dialog.show();
//        mButton.setBackgroundDrawable(MyApplication.getInstance()
//                .getResources().getDrawable(R.drawable.shape_button_red));

    }

    @SuppressWarnings("deprecation")
    public void setButtonUnEnable(long millisUntilFinished) {
        Date date = new Date(millisUntilFinished);
        System.out.println(date);
        mButton.setEnabled(false);
        int hours = (int) ((millisUntilFinished) / 3600000);
        int minutes = (int) (((millisUntilFinished) / 1000 - hours * 3600) / 60);
        int second = (int) ((millisUntilFinished) / 1000 - hours * 3600 - minutes * 60);
        String hors = hours + "";
        String minu = minutes + "";
        String sec = second + "";
        if (!(hours > 9)) {
            hors = "0" + hours;
        }
        if (!(minutes > 9)) {
            minu = "0" + minutes;
        }
        if (!(second > 9)) {
            sec = "0" + second;
        }
        mButton.setText(hors + ":" + minu + ":" + sec);
    }

}
